package com.yh.csx.business.api.entity;

import com.yh.csx.bsf.core.util.PropertyUtils;
import com.yh.csx.business.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;


/**
 * @创建人 霍钧城
 * @创建时间 2020年12月10日 14:51:00
 * @描述 分页请求
 */
@Slf4j
public class Pagination {
    private Integer pageNum;
    private Integer pageSize;
    private boolean count = true;

    private static final String bsfMaxPageSize="bsf.business.page.maxPageSize";

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        if (pageNum != null && pageNum <= 0) {
            throw new BusinessException("分页页码必须大于0");
        }
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize != null) {
            int maxPageSize=PropertyUtils.getPropertyCache(bsfMaxPageSize,1000);
            if (pageSize > maxPageSize) {
                log.warn("[Pagination.pageSize]超出设定最大页：{}，请检查确认",pageSize);
                pageSize = maxPageSize;
            } else if (pageSize <= 0) {
                throw new BusinessException("分页大小必须大于0");
            }
        }
        this.pageSize = pageSize;
    }

    public boolean isCount() {
        return count;
    }

    public void setCount(boolean count) {
        this.count = count;
    }

    public boolean isPageable(){
        return this.pageNum != null && this.pageNum > 0 && this.pageSize != null && this.pageSize > 0;
    }
}
